package com.totcy.atom.atom.video;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;

import org.junit.Test;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.MediaType;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class OkHttp131 {

    private static String token = "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJhdWQiOiJ3ZWIiLCJleHAiOjE1NDg4MjgyNzYsImlhdCI6MTU0ODc0MTg3NiwiaXNzIjoiaHVtb3JsaWFuZyIsImp0aSI6MTU0ODc0MTg3Niwic3ViIjoiYWxsIiwidXNlcl9pZCI6Mn0.bWsKtf4BZwLOWFIk-B4cz06KZUE7fAV_uT5IsWRLSAk";
    private static List<String> listAVIdTemp = new ArrayList<>();
    private static String avidAllStr = "";
    private static String fileName = System.getProperty("user.dir") + "/src/test/java/com/totcy/atom/atom/video/UploadAVList.txt";

    @Test
    public void mainRes() {

        String result = getVideoList(-1, "86C0944C2F4A72DF03684BA47B11763B");
//        String result = getVideoList(5678, "B17D621517890747DB6B0532B202AB23");
//        String result = getVideoList(5658, "EFECB93EF39A4D54FF74AEFDEE315359");
//        String result = getVideoList(5618, "040F5E1397ADF0C3365010BD8C89B8F2");
//        String result = getVideoList(5598, "306033E3D480DD85588D2F9D3BC02C3A");
//         String result = getVideoList(5578, "8495419BB9A72BD37BBD747632F98AC1");
//         String result = getVideoList(5558, "432AE311FEB3132263572F986F217975");
//        String result = getVideoList(5538, "1F50CDC1DFA22689DAE3DBC3495818D3");
//        String result = getVideoList(5518, "7E4D0755A47617E45857275A8C87F5F6");
//        String result = getVideoList(5498, "9AA3960C4243C46F6ED2D9D0DE94F6A2");


        List<VideoBean.NewVideoListBean> videoBeanList = convertResponse(result);

        //读取文件中的avid
        try {
            avidAllStr = FileUtils.readFile(fileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        listAVIdTemp.clear();
        for (VideoBean.NewVideoListBean videoBean : videoBeanList) {
            uploadVideoInfo(videoBean);
        }

        //保存avid
        try {
            FileUtils.writeFile(listAVIdTemp, fileName);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static String getVideoList(int lastIndex, String sign) {
        OkHttpClient client = new OkHttpClient();

        Request request = new Request.Builder()
                .url("http://api.mm131.tv:88/mm131/getNewVideoList21?lastIndex=" + lastIndex)
                .get()
                .addHeader("sign", sign)
                .addHeader("token", "eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJ1aWQiOjIzNjEyNDU5NTMxLCJyYW5kb20iOjE1NDgxNDM4Nzc2OTksImxvZ2luX3R5cGUiOjF9.Zql9mfJHAXwMOhmqodftvSGLYmnnjSnUs00xDIiTiTg")
                .addHeader("applicationid", "1")
                .addHeader("api_version", "v20")
                .addHeader("did", "99000535484409,,76adcbac68b39ffa")
                .addHeader("appkey", "06E0C9C16A169AEC")
                .addHeader("channel", "GW_4")
                .addHeader("client_version", "1.7.9")
                .build();

        try {
            Response response = client.newCall(request).execute();

            //将响应数据转化为输入流数据
            if (response.isSuccessful()) {
                return response.body().string();
            } else {
                System.err.println("http code = " + response.code() + " : " + response.message());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }

    public static List<VideoBean.NewVideoListBean> convertResponse(String json) {

        Gson gson = new Gson();

        BaseVideoResponse<VideoBean> o = gson.fromJson(
                json,
                new TypeToken<BaseVideoResponse<VideoBean>>() {
                }.getType());

        return o.getDataObj().getNewVideoList();
    }

    public static void uploadVideoInfo(VideoBean.NewVideoListBean bean) {
        //读取文件中的avid，存在return。
        if (avidAllStr.contains(bean.getAvid())) {
            System.err.println(bean.getAvid() + "资源存在！！！");
            return;
        }

        JsonObject jsonObject = new JsonObject();
        jsonObject.addProperty("link_name", bean.getAvid());
        jsonObject.addProperty("link_url", bean.getVideo_url());
        jsonObject.addProperty("link_description", bean.getTitle());
        String coverPath = bean.getVideo_url().substring(0, bean.getVideo_url().lastIndexOf("/"));
        coverPath = coverPath + bean.getCover_url();
        jsonObject.addProperty("link_image_url", coverPath);

        //保存上传的avid 下次做去重
        listAVIdTemp.add(bean.getAvid());

        OkHttpClient client = new OkHttpClient();

        MediaType mediaType = MediaType.parse("application/json");
        RequestBody body = RequestBody.create(mediaType, jsonObject.toString());
        Request request = new Request.Builder()
                .url("http://59.110.163.168:9090/v1/admin/link")
                .post(body)
                .addHeader("authorization", token)
                .addHeader("content-type", "application/json")
                .build();

        try {
            Response response = client.newCall(request).execute();
            //将响应数据转化为输入流数据
            if (response.isSuccessful()) {
                System.err.println("资源：" + bean.getAvid() + " 上传成功");
            } else {
                System.err.println("http code = " + response.code() + " : " + response.message());
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    @Test
    public void testFileRW() {
        List<String> list = new ArrayList<>();
        list.add("1");
        list.add("2");
        list.add("3");
        list.add("4");

        String fileName = System.getProperty("user.dir") + "/src/test/java/com/totcy/atom/atom/video/UploadAVList.txt";

        try {
            FileUtils.writeFile(list, fileName);

        } catch (IOException e) {
            e.printStackTrace();
        }

        //读
        try {
            String s = FileUtils.readFile(fileName);
            System.err.println(s);
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    @Test
    public void deleteLink() {

        JsonObject jsonObject = new JsonObject();
        for (int i = 1; i < 239; i++) {
            jsonObject.addProperty("link_id", i);
            OkHttpClient client = new OkHttpClient();

            MediaType mediaType = MediaType.parse("application/json");
            RequestBody body = RequestBody.create(mediaType, jsonObject.toString());
            Request request = new Request.Builder()
                    .url("http://59.110.163.168:9090/v1/admin/link")
                    .delete(body)
                    .addHeader("authorization", token)
                    .addHeader("content-type", "application/json")
                    .build();

            try {
                Response response = client.newCall(request).execute();
                //将响应数据转化为输入流数据
                if (response.isSuccessful()) {
                    System.err.println("删除成功");
                } else {
                    System.err.println("http code = " + response.code() + " : " + response.message());
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }

    }
}
